Introduction to R and RStudio

GVPT399F: Power, Politics, and Data

Learning objectives for today

  1. Introduction to R

  2. Introduction to RStudio

Why R?

  • A versatile programming language

    • All materials used in this course were produced using R, including the website and fancy slides
  • Free and accessible!

R and RStudio

R

R is a free and open-source programming language and software environment for statistical computing and graphics.

RStudio

RStudio is an integrated development environment (IDE) for the R programming language.

A Tour of RStudio

Source: R4DS

R basics

R code:

1 + 2
[1] 3

Functions:

sum(1, 2)
[1] 3

EXERCISE

  1. Open up RStudio.
  2. Using the console, find the summation of 45, 978, and 121.
  3. What is 67 divided by 6?
  4. What is the square root of 894? HINT: use the sqrt() function.

CHECK YOUR ANSWERS

Using the consol, find the summation of 45, 978, and 121.

sum(45, 978, 121)
[1] 1144

Or:

45 + 978 + 121
[1] 1144

What is 67 divided by 6?

67 / 6
[1] 11.16667

What is the square root of 894?

sqrt(894)
[1] 29.89983

Functions

R packages

Packages are collections of R functions and data.

# Install the relevant package(s)
install.packages("tidyverse")

# Load the packages in current session
library(tidyverse)

EXERCISE

  1. Open up RStudio.
  2. Using the console, install the tidyverse packages.
install.packages("tidyverse")
  1. Load these packages in your current session
library(tidyverse)

RStudio Projects

For your sanity’s sake, for your co-author’s sanity’s sake

Keeps everything:

  • Organised

  • Reproducible

  • Sustainable

EXERCISE

  1. In the RStudio console, type in getwd() to see where you are on your computer.

EXERCISE

  1. Create a new RStudio project for this course:

Source: R4DS

EXERCISE

  1. Create a new RStudio project for this course:

Source: R4DS

EXERCISE

  1. Create a new RStudio project for this course:

Source: R4DS

Summary

This session you:

  1. Learnt about new data science tools to help you conduct replicable and reproducible political science research

  2. Set up your data science tools